package cn.aran.order.service.impl;

import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.service.impl.BaseServiceImpl;
import cn.aran.basic.utils.CodeGenerateUtils;
import cn.aran.basic.utils.DistanceUtil;
import cn.aran.basic.utils.Point;
import cn.aran.order.domain.OrderProduct;
import cn.aran.order.domain.OrderProductAcquisition;
import cn.aran.order.mapper.OrderProductAcquisitionMapper;
import cn.aran.order.mapper.OrderProductMapper;
import cn.aran.order.service.IOrderProductService;
import cn.aran.order.vo.HandleOrderVo;
import cn.aran.org.domain.Shop;
import cn.aran.org.mapper.ShopMapper;
import cn.aran.pet.mapper.PetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aran
 * @since 2022-07-21
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {
    @Autowired
    private OrderProductMapper orderProductMapper;

    @Autowired
    private ShopMapper shopMapper;
    
    @Autowired
    private OrderProductAcquisitionMapper orderProductAcquisitionMapper;
    
    @Autowired
    private PetMapper petMapper;

    @Override
    public void order(OrderProduct orderProduct) {
        // 保存订单信息
        orderProductMapper.save(orderProduct);
        // 获取位置坐标
        Point point = DistanceUtil.getPoint(orderProduct.getAddress());
        // 获取附近的店铺信息
        List<Shop> shops = shopMapper.loadAll();
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        if (nearestShop != null){
            orderProduct.setShopId(nearestShop.getId());
            // 发送信息告知店铺
        }
        orderProductMapper.update(orderProduct);
    }

    /**
    * @Title: accept
    * @Description: 店铺员工接单
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 15:17
    * @Parameters: [handleOrderVo]
    * @Return void
    */
    @Override
    public void accept(HandleOrderVo handleOrderVo) {
        OrderProductAcquisition orderProductAcquisition = orderProductAcquisitionMapper.loadByOrderReceived(handleOrderVo.getMsgId());
        // 如果查询到了收购单，那么说明此消息已被接单，提示用户
        if (orderProductAcquisition != null){
            throw new BusinessException("此消息已被接单！");
        }
        // 1.查询寻主消息
        OrderProduct orderProduct = orderProductMapper.loadById(handleOrderVo.getMsgId());
        // 3.初始化订单对象并保存
        orderProductAcquisition = initOrderProductAcquisition(handleOrderVo, orderProduct);
        orderProductAcquisitionMapper.save(orderProductAcquisition);
    }

    @Override
    public void reject(Long msgId) {
        // 1.参数校验
        // 2.根据消息Id查询寻主消息，将shopId置空，并修改
        OrderProduct orderProduct = orderProductMapper.loadById(msgId);
        if (orderProduct == null){
            throw new BusinessException("此订单不存在！");
        }
        orderProduct.setShopId(null);
        orderProductMapper.update(orderProduct);
        // 3.记录日志，自行完成
    }


    private OrderProductAcquisition initOrderProductAcquisition(HandleOrderVo handleOrderVo, OrderProduct orderProduct) {
        OrderProductAcquisition orderProductAcquisition = new OrderProductAcquisition();
        // 使用工具类生成订单编号
        String orderSn = CodeGenerateUtils.generateOrderSn(orderProduct.getUserId());
        orderProductAcquisition.setOrderSn(orderSn);
        orderProductAcquisition.setDigest(handleOrderVo.getNote());
        // 最迟的处理订单的时间
        orderProductAcquisition.setLastcomfirmtime(new Date(System.currentTimeMillis() + 2*24*60*60*1000));
        // 这时候还不是最终价格，因为我们销售人员可以去讲价，加价之后的才是最终价格
        orderProductAcquisition.setPrice(orderProduct.getPrice());
        orderProductAcquisition.setProductId(orderProduct.getProductId());
        orderProductAcquisition.setUserId(orderProduct.getUserId());
        orderProductAcquisition.setShopId(orderProduct.getShopId());
        orderProductAcquisition.setAddress(orderProduct.getAddress());
        orderProductAcquisition.setEmpId(handleOrderVo.getHandler());
        orderProductAcquisition.setSearchMasterMsgId(orderProduct.getId());
        return orderProductAcquisition;
    }


}
